package project7.Storage;

import java.util.ArrayList;
// import java.util.Date;

/**
 * This class represents a server in project7.
 * A server can be associated with many tables,
 * and has an array list of all tables.
 *
 */
public class Server implements Comparable{
    
    /*------ Private Variables -----------------------------------------------*/
    
    private int code = 0;
    private String lastName = "";
    private String firstName = "";
    private String shiftType;
    private ArrayList<Table> openTables = null;
    private int numberOpenTables = 0;
    private double totalSales = 0.0;

    /*------ Constructors ----------------------------------------------------*/
    
    /**
     * This constructs a new Server object
     * @param c int of the Server's id number
     * @param last Server's last name
     * @param first Server's first name
     */
    public Server(int c, String last, String first){
        code = c;
        lastName = last;
        firstName = first;
        shiftType = "PM";
        openTables = new ArrayList<Table>();
    }

    /**
     * This adds a table to this server's list of open tables
     * @param table Table to add
     */
    public void addTable(Table table){
        openTables.add(table);
        numberOpenTables = openTables.size() - 1;
    }

    /**
     * This removes a table from the server's table list
     * @param tableNumber int of Table to remove
     */
    public void removeTable(int tableNumber){
//        openTables.remove(new Table(tableNumber));
        for(Table T : openTables)
            if(T.getNumber() == tableNumber)
                openTables.remove(T);
    }

    /*------ Accessors and Mutators ------------------------------------------*/

    /**
     * This returns the Server's ID number, aka Code.
     * @return int of the Servcer's ID
     */
    public int getCode(){ return code; }
    /**
     * This changes the ID to the int c
     * @param c new ID number for the Server.
     */
    public void setCode(int c){ code = c; }

    /**
     *
     * @return String of Server's last name
     */
    public String getLastName(){ return lastName; }
    /**
     *
     * @param last new last name to set
     */
    public void setLastName(String last){ lastName = last; }

    /**
     *
     * @return String of Server's first name
     */
    public String getFirstName(){ return firstName; }
    /**
     *
     * @param first new first name to set
     */
    public void setFirstName(String first){ firstName = first; }

    /**
     *
     * @return String of when the shift starts
     */
    public String getShiftStart(){ return shiftType; }
    /**
     *
     * @param type change the start of shift to type
     */
    public void setShiftStart(String type){ shiftType = type; }

    /**
     *
     * @return ArrayList of all Tables associated with this Server
     */
    public ArrayList<Table> getOpenTables(){ return openTables; }
    /**
     *
     * @param oTables reassign the server to the tables contained in oTables
     */
    public void setOpenTables(ArrayList<Table> oTables){
        openTables = oTables;
        numberOpenTables = openTables.size() - 1;
    }

    /**
     * THIS IS DEPRECIATED
     * @return int of # of tables server is currently serving
     */
    public int getNumberOpenTables(){ return numberOpenTables; }
    /**
     * THIS IS DEPRECIATED
     * @param nTables change number of open tables the server has to nTables
     */
    public void setNumberOpenTables(int nTables){ numberOpenTables = nTables; }

    /**
     *
     * @return double of total sales by this Server
     */
    public double getTotalSales(){ return totalSales; }
    /**
     *
     * @param tSales
     */
    public void setTotalSales(double tSales){ totalSales = tSales; }

    /*------ Overrides -------------------------------------------------------*/

    /**
     *
     * @return String of Server data
     */
    @Override
    public String toString(){
        String str= "";

        str = "Name = " + lastName + ", " + firstName + "\n";

        for(int i = 0; i < openTables.size(); i++){
            str += openTables.get(i).toString() + "\n";
        }
        str += "Total Sales = " + totalSales;

        return str;
    }

    /**
     * Natural order Comparator
     * @param o object to compare to
     * @return 0 of they are the same, 1 if this comes after the object, -1 if this comes before
     */
    public int compareTo(Object o) {
        if (o == null) return -1;
        if (!this.getClass().equals(o.getClass())) return -1;
        Server temp = (Server) o;
        return lastName.compareTo(temp.getLastName());
    }

}
